from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from fw import verifier
import time


def goto_main_page(browser):
    if not verifier.is_element_present(browser, By.XPATH, ".//div[@class='searchbox']/input[@value='Search for movies...']"):
        browser.find_element(By.XPATH, ".//h1[text()='My collection']").click()


def wait_for_ajax(browser):
    for i in range(30):
        try:
            if browser.execute_script('return jQuery.active == 0'):
                break
        except:
            pass
        time.sleep(1)
    else:
        raise TimeoutException('wait for jQuery.active == 0 timed out')
